import * as userApi from '@/api/home.js';
import { addShoppingCartList, updateShoppingCartNum } from '../../api/home.js';
export default {
  namespaced: true,
  state() {
    return {
      homeNavList: [],
      shoppingCartList: [],
      homeBannerList: [],
      homeNewList: [],
      homeHotList: [],
      homeBrandList: [],
      homegoodList: [],
      homeSpecialList: [],
      delShoppingCartMsg: '',
      addShoppingCartMsg: '',
      updateShoppingCartNum: '',
    };
  },
  mutations: {
    SET_HOME_LIST(state, newValue) {
      state.homeNavList = newValue;
    },
    SET_SHOPPING_LIST(state, newValue) {
      state.shoppingCartList = newValue.filter((item) => item.isEffective);
    },
    SET_SHOPPING_ALL_LIST(state, newValue) {
      state.shoppingCartList = newValue;
    },
    SET_SHOPPING_DEL_RES(state, newValue) {
      state.delShoppingCartMsg = newValue;
    },
    SET_SHOPPING_UPDATE_RES(state, newValue) {
      state.updateShoppingCartNum = newValue;
    },
    SET_SHOPPING_ADD_RES(state, newValue) {
      state.addShoppingCartMsg = newValue;
    },
    SET_HOMEBANNER_LIST(state, newValue) {
      state.homeBannerList = newValue;
    },
    SET_HOMENEW_LIST(state, newValue) {
      state.homeNewList = newValue;
    },
    SET_HOMEHOT_LIST(state, newValue) {
      state.homeHotList = newValue;
    },
    SET_HOMEBRAND_LIST(state, newValue) {
      state.homeBrandList = newValue;
    },

    SET_HOMEGOOD_LIST(state, newValue) {
      state.homegoodList = newValue;
    },
    SET_HOMESPECIALIST_LIST(state, newValue) {
      state.homeSpecialList = newValue;
    },
  },
  actions: {
    // 首页导航
    async updateHomeNavList({ commit }, params) {
      const res = await userApi.getHomeNavList(params);
      commit('SET_HOME_LIST', res);
    },
    // 购物车
    async updateShoppingCartList({ commit }, params) {
      const res = await userApi.getShoppingCartList(params);
      commit('SET_SHOPPING_LIST', res);
    },
    // 购物车获取全部
    async getAllShoppingCartList({ commit }, params) {
      const res = await userApi.getShoppingCartList(params);
      commit('SET_SHOPPING_ALL_LIST', res);
    },
    // 购物车 删除商品
    async delShoppingCartList({ commit }, params) {
      const res = await userApi.delShoppingCartList(params);
      commit('SET_SHOPPING_DEL_RES', res);
    },
    // 购物车 增加商品
    async addShoppingCartList({ commit }, params) {
      const res = await userApi.addShoppingCartList(params);
      commit('SET_SHOPPING_ADD_RES', res);
    },
    // 购物车 修改商品数量
    async updateShoppingCartNum({ commit }, params) {
      const res = await userApi.updateShoppingCartNum(params);
      commit('SET_SHOPPING_UPDATE_RES', res);
    },
    // 轮播图
    async updateHomeBannerList({ commit }, params) {
      const res = await userApi.getHomeBanner(params);
      commit('SET_HOMEBANNER_LIST', res);
    },
    // 新鲜好物
    async updateHomeNewList({ commit }, params) {
      const res = await userApi.getHomeNew(params);
      commit('SET_HOMENEW_LIST', res);
    },
    // 人气推荐
    async updateHomeHotList({ commit }, params) {
      const res = await userApi.getHomeHot(params);
      commit('SET_HOMEHOT_LIST', res);
    },
    // 热门品牌
    async updateHomeBrandList({ commit }, params) {
      const res = await userApi.getHomeBrand(params);
      commit('SET_HOMEBRAND_LIST', res);
    },
    // 产品区块
    async updateHomegoodList({ commit }, params) {
      const res = await userApi.getHomeGoods(params);
      commit('SET_HOMEGOOD_LIST', res);
    },
    // 最新专区
    async updateHomeSpecialList({ commit }, params) {
      const res = await userApi.getHomeSpecial(params);
      commit('SET_HOMESPECIALIST_LIST', res);
    },
  },
};
